#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include "pthread.h"
#include "project.h"
#include "util.h"
#include "getMemory.h"

#ifdef TEST_DELETE_LEAST_BUFFER
 #include "binarybuffer.h"
#endif

#ifdef TEST_DELETE_LEAST_BUFFER
	F_FILE root = NULL;
	void init();
	F_FILE insert(const char*,int,time_t,F_FILE);
	void show(F_FILE);
#endif

int
main (int argc, char *argv[])
{
#ifdef TEST_DELETE_LEAST_BUFFER
	init();
//	char *delete_name = Find_Delete_LeastBuf_Proxy(&root, 0);
//	printf("delete node's name: %s\n", delete_name);
//	free(delete_name);
//	show(root);
//	file_node_invalid(root, time(NULL));
	show(root);
	return 0;
#endif
    
    init_tree();

	DIR *dp;
	struct dirent *entry;
//	dp = opendir(argv[1]);
    dp = opendir(FILEPATH);

	while ((entry = readdir(dp)) != NULL)
	{
        //avoid self and parent directory
		if (entry->d_name[0] == '.')
			continue;
		long size = get_file_size(entry->d_name);
		if (size <= 0)
		{
			fprintf(stderr, "file not exists");
			continue;
		}
	
		/* 循环读取文件 */
		char *str;				/* 模拟一个指针 */
		int buf_size;
		for (;;)
		{
			buf_size = f_read(&str,0);
			//文件读取完毕，继续执行下一次查询，这里先模拟一次
			if (buf_size == 0)
			{
				f_read(&str, 1);		/* 当文件传送结束就发送信号回来，通知程序释放内存 */
				break;
			}
            //printf("%s\n", str);
		}
	}

	/* 模拟一次缓存查询 */
	long size = get_file_size("zzzz");
	if (size <= 0)
	{
		fprintf(stderr, "file not exists");
		exit(-1);
	}
	printf("file name: zzzz; size: %ld\n", size);
	
	char *str;	
	int buf_size;
	for (;;)
	{
		buf_size = f_read(&str,0);
		if (buf_size == 0)
		{
			f_read(&str, 1);
			break;
		}
		printf("%s\n", str);
	}

	return 0;
}

/*
 * all next codes is for test
 */

#ifdef TEST_DELETE_LEAST_BUFFER
void
init ()
{
    root = NULL;
	char *str[12] = 
	{"erd", "geafe", "jkfewo", "fdae", "yhjd", "iwort", "ienv", "qop", "fdjioeion", "fsq", "pyir", "qpei"};

	root = insert(str[0], 4, 1350817803L, root);
	insert(str[1], 10, 		 1350821413L, root);
	insert(str[2], 8, 		 1350821423L, root);
	insert(str[3], 1, 		 1350821433L, root);
	insert(str[4], 2, 		 1350821443L, root);
	insert(str[5], 3, 		 1350821453L, root);
	insert(str[6], 56, 		 1350821463L, root);
	insert(str[7], 7, 		 1350821473L, root);
	insert(str[8], 9, 		 1350821483L, root);
	insert(str[9], 12, 		 1350821493L, root);
	insert(str[10], 41, 	 1350821445L, root);
	insert(str[11], 31, 	 1350821456L, root);
}
#endif

//#ifdef TEST_DELETE_LEAST_BUFFER
//F_FILE
//insert (const char *key_name, int acount, time_t laccess, F_FILE T)
//{
//	if (T == NULL)
//	{
//		T = (F_FILE)malloc(sizeof(struct f_file));
//		if (T == NULL)
//		{
//			sleep(1);
//			T = (F_FILE)malloc(sizeof(struct f_file));
//		}
//		if (T == NULL)
//		{
//			fprintf(stderr, "Out of Memory\n");
//			exit(3);
//		}
//
//		T->fl_name = strdup(key_name);
//		T->fl_count = acount;
//		T->last_access = laccess;
//		/* Get file size */
//		T->fl_size = 0;
//		T->fl_buf = NULL;
//		T->left = T->right = NULL;
//	}
//	else
//	{
//		int key_len = strlen(key_name);
//		int t_len = strlen(T->fl_name);
//		int len = MAX(key_len, t_len);
//		if (memcmp(key_name, T->fl_name, len) < 0)
//			T->left = insert(key_name, acount, laccess, T->left);
//		else if (memcmp(key_name, T->fl_name, len) > 0)
//			T->right = insert(key_name, acount, laccess, T->right);
//	}
//
//	return T;
//}
//#endif

#ifdef TEST_DELETE_LEAST_BUFFER
F_FILE
insert (const char *key_name, int acount, time_t laccess, F_FILE T)
{
    F_FILE temp = (F_FILE) malloc (sizeof(struct f_file));
    temp->fl_name = strdup(key_name);
    temp->fl_count = acount;
    temp->last_access = laccess;
    temp->fl_size = 0;
    temp->fl_buf = NULL;
    temp->left = temp->right = NULL;
    if (!T)
    {
        T = temp;
        return T;
    }
    else
    {
        F_FILE p = T, prev;
        while (p != NULL)
        {
            prev = p;
            p = (strcmp(key_name, p->fl_name) < 0 ? p->left : p->right);
        }
        if (strcmp(key_name, prev->fl_name) < 0)
            prev->left = temp;
        else
            prev->right = temp;

        return temp;
    }
}
#endif

#ifdef TEST_DELETE_LEAST_BUFFER
void
show (F_FILE T)
{
	if (T == NULL)
		return;

	show(T->left);
	printf("name: %s; count: %u; last access time: %ld\n", T->fl_name, T->fl_count, T->last_access);
	show(T->right);
}
#endif